package com.library.common.widget

import android.content.Context
import android.util.AttributeSet
import com.lcodecore.tkrefreshlayout.IBottomView
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.lcodecore.tkrefreshlayout.footer.LoadingView

/**
 * Author：SimGa
 * CoryRight：2018/8/23
 * PackageName：cn.ewhale.huaxinebought.com.vitabuybuy.tv.widget
 * 使用时记得给子布局加上android:overScrollMode="never"   ScrollView  recycleView ListView GridView这些列表类
 * 还有更多的操作查询父类
 */
class CoustomRefreshView : TwinklingRefreshLayout {
    private var headView: RefreshViewHolder? = null
    private var footView: IBottomView? = null

    constructor(context: Context?) : super(context, null) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context,
        attrs,
        0
    ) {
        initView()
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        headView = RefreshViewHolder(context)
        footView = LoadingView(context)
        setOverScrollBottomShow(false)
        setOverScrollTopShow(false)
        addHeadView(headView)
        addFootView(footView)
    }

    //设置刷新头布局
    fun addHeadView(headView: RefreshViewHolder?) {
        this.headView = headView
        setHeaderView(headView)
    }

    //设置加载底部布局
    fun addFootView(footView: IBottomView?) {
        this.footView = footView
        setBottomView(footView)
    }

    //关闭刷新
    fun finishRefresh() {
        finishRefreshing()
    }

    //关闭加载更多
    fun finishloadingMore() {
        finishLoadmore()
    }

    //同时关闭加载更多下拉刷新布局
    fun finishRefreshLoadingMore() {
        finishRefreshing()
        finishLoadmore()
    }

    //是否禁止下拉刷新操作
    fun enabelRefresh(enable: Boolean) {
        setEnableRefresh(enable)
    }

    //是否禁止上拉加载操作
    fun enableLoadMore(enable: Boolean) {
        setEnableLoadmore(enable)
    }

    fun onLoad(size: Int) {
        finishRefreshLoadingMore()
        if (size < 10) {
            enableLoadMore(false)
        } else {
            enableLoadMore(true)
        }
    }
}